package cn.xz.zysjz.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Objects;

import cn.xz.zysjz.R;
import cn.xz.zysjz.activity.AboutActivity;
import cn.xz.zysjz.activity.AddYActivity;
import cn.xz.zysjz.activity.AdminFKActivity;
import cn.xz.zysjz.activity.EditActivity;
import cn.xz.zysjz.activity.GLSJActivity;
import cn.xz.zysjz.activity.GlYonghuActivity;
import cn.xz.zysjz.activity.HistActivity;
import cn.xz.zysjz.activity.KfActivity;
import cn.xz.zysjz.activity.LikeActivity;
import cn.xz.zysjz.activity.MyActivity;
import cn.xz.zysjz.activity.PhoneActivity;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.bean.MessageEvent;
import cn.xz.zysjz.bean.SJBean;
import cn.xz.zysjz.bean.UserBean;
import cn.xz.zysjz.databinding.FragmnetMineBinding;
import cn.xz.zysjz.dialog.ShowDialog;
import cn.xz.zysjz.utils.AppHelper;
import cn.xz.zysjz.utils.DataCleanManager;
import cn.xz.zysjz.utils.SessionUtils;
import cn.xz.zysjz.utils.ToastUtils;

/**
 * 我的模块*/
public class MineFragment extends Fragment {

    private FragmnetMineBinding mBinding;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragmnet_mine, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (CommonLib.nowUserBean != null) {
            //头像
            if (!Objects.equals(CommonLib.nowUserBean.getPic(), "")){
                AppHelper.setPic(mBinding.ivHead,CommonLib.nowUserBean.getPic());
            }
            //用户名
            mBinding.tvName.setText(CommonLib.nowUserBean.getName().toString());
            //性别
            String mSex = "性别: 男";
            if (Objects.equals(CommonLib.nowUserBean.getSex(), "0")){
                //男
                mSex = "性别: 女";
            }
            mBinding.tvSex.setText(mSex);
            //年龄
            mBinding.tvOld.setText("年龄" + CommonLib.nowUserBean.getOld());

            //是不是商家
            if (CommonLib.nowUserBean.getSj() != null && CommonLib.nowUserBean.getSj()){
                mBinding.viewAddL1.setVisibility(View.VISIBLE);
                mBinding.llcAddL1.setVisibility(View.VISIBLE);

                mBinding.viewSj.setVisibility(View.GONE);
                mBinding.llcSj.setVisibility(View.GONE);
            }else {
                mBinding.viewSj.setVisibility(View.VISIBLE);
                mBinding.llcSj.setVisibility(View.VISIBLE);

                mBinding.viewAddL1.setVisibility(View.GONE);
                mBinding.llcAddL1.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //管理员都不显示
        mBinding.cvHead.setVisibility(View.VISIBLE);
        mBinding.llcTop.setVisibility(View.VISIBLE);
        mBinding.llcB.setVisibility(View.VISIBLE);
        mBinding.llcAdminMine.setVisibility(View.GONE);
        if (CommonLib.isAdmin){
            mBinding.cvHead.setVisibility(View.GONE);
            mBinding.llcTop.setVisibility(View.GONE);
            mBinding.llcB.setVisibility(View.GONE);
            mBinding.llcAdminMine.setVisibility(View.VISIBLE);
        }
        //获取缓存
        try {
            mBinding.tvChe.setText(DataCleanManager.getTotalCacheSize(getContext()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //编辑信息
        mBinding.llcEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppHelper.toJump(EditActivity.class);
            }
        });
        //手机号
        mBinding.llcPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳到手机号界面
                AppHelper.toJump(PhoneActivity.class);
            }
        });
        //客服
        mBinding.llcKf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到在线联系
                AppHelper.toJump(KfActivity.class,"客服-小石");
            }
        });
        //收藏列表
        mBinding.llcHit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳到预约记录
                AppHelper.toJump(LikeActivity.class);
            }
        });
        //添加内容
        mBinding.llcAddL.setOnClickListener(v->{
            AppHelper.toJump(AddYActivity.class);
        });
        //管理用户
        mBinding.llcGy2h.setOnClickListener(v->{
            AppHelper.toJump(GlYonghuActivity.class);
        });
        //清理缓存
        mBinding.llcChe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowDialog.show1("确定清除缓存吗？", getActivity(), new ShowDialog.OnShowClick() {
                    @Override
                    public void click() {
                        DataCleanManager.clearAllCache(getContext());
                        mBinding.llcChe.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                //获取缓存
                                try {
                                    mBinding.tvChe.setText(DataCleanManager.getTotalCacheSize(getContext()));
                                } catch (Exception e) {
                                    throw new RuntimeException(e);
                                }
                                ToastUtils.show("清除成功");
                            }
                        },1500);
                    }
                });
            }
        });
        //退出
        mBinding.llcFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowDialog.show1("确定退出应用吗？", getActivity(), new ShowDialog.OnShowClick() {
                    @Override
                    public void click() {
                        EventBus.getDefault().post(new MessageEvent("关闭应用"));
                    }
                });
            }
        });
        mBinding.llcFinishAdmin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowDialog.show1("确定退出应用吗？", getActivity(), new ShowDialog.OnShowClick() {
                    @Override
                    public void click() {
                        EventBus.getDefault().post(new MessageEvent("关闭应用"));
                    }
                });
            }
        });
        //成为商家
        mBinding.llcSj.setOnClickListener(v->{
            ShowDialog.show3(getActivity(), new ShowDialog.OnShowClickStr() {
                @Override
                public void click(String s) {
                    ArrayList<SJBean> list = SessionUtils.getList(CommonLib.SJJ_L, SJBean.class);
                    boolean dd = false;
                    for (SJBean sjBean: list){
                        if (Objects.equals(sjBean.getName(), CommonLib.nowUserBean.getName())){
                            dd = true;
                        }
                    }
                    if (dd){
                        ToastUtils.show("不可重复申请");
                    }else {
                        SJBean sjBean = new SJBean();
                        sjBean.setName(CommonLib.nowUserBean.getName());
                        sjBean.setGuo(false);
                        sjBean.setMas(s);
                        sjBean.setUserBean(CommonLib.nowUserBean);
                        list.add(sjBean);
                        SessionUtils.commitList(CommonLib.SJJ_L,list);

                        ToastUtils.show("申请成功，请耐心等待");
                    }

                }
            });
        });
        //添加商品
        mBinding.llcAddL1.setOnClickListener(v->{
            AppHelper.toJump(AddYActivity.class);
        });
        //商家管理
        mBinding.llcSjgl.setOnClickListener(v->{
            AppHelper.toJump(GLSJActivity.class);
        });
        //关于我们
        mBinding.llcAbout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppHelper.toJump(AboutActivity.class);
            }
        });
        //意见反馈
        mBinding.llcFk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowDialog.show3(getActivity(), new ShowDialog.OnShowClickStr() {
                    @Override
                    public void click(String s) {
                        ArrayList<String> list = SessionUtils.getList(CommonLib.USER_FK, String.class);
                        list.add(s);
                        SessionUtils.commitList(CommonLib.USER_FK,list);
                        ToastUtils.show("我们已收到您的反馈");
                    }
                });
            }
        });
        //意见反馈列表
        mBinding.llcFkAdmin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppHelper.toJump(AdminFKActivity.class);
            }
        });
    }
}
